<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column
                type="index"
                align="center"
                label="序号"
                prop="id"
                width="120"
              />
              <el-table-column
                align="center"
                label="角色名称"
                prop="name"
                width="240"
              />
              <el-table-column align="center" label="描述" prop="description" />
              <el-table-column align="center" label="操作" prop="companyId">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="btnPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="setRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="delRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <!-- <el-pagination
                layout="prev,pager,next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
              /> -->
              <el-pagination
                background
                layout="prev, pager, next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input
                  disabled
                  style="width:400px"
                  v-model="formData.name"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  style="width:400px"
                  v-model="formData.companyAddress"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width:400px"
                  v-model="formData.mailbox"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                  v-model="formData.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 放置一个弹层组件 -->
    <el-dialog title="编辑角色" :visible="showDialog" @close="btnclose">
      <el-form
        :model="roleForm"
        label-width="120px"
        :rules="rules"
        ref="roleForm"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="roleForm.name"
            style="width:80%"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            v-model="roleForm.description"
            style="width:80%"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnclose">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 控制权限分配弹层 -->
    <el-dialog
      title="权限分配"
      :visible="premDialog"
      @close="premDialog = false"
    >
      <el-tree
        :data="premData"
        :props="premProp"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
        :default-checked-keys="selectCheck"
        ref="premTree"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPremOk"
            >确定</el-button
          >
          <el-button size="small" @click="premDialog = false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  delRole,
  getRoleDetail,
  updataRole,
  addRole,
  assignPerm
} from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  data () {
    return {
      list: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2,
        total: 0 // 记录总数
      },
      formData: {}, // 公司信息
      showDialog: false, //弹层
      roleForm: {
        name: '',
        description: ''
      }, // 角色
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      premDialog: false, // 控制权限分配弹层
      premData: [], //接收权限数据
      premProp: {
        label: 'name'
      },
      userId: null, // 记录当前点击的id
      selectCheck: [] // 记录当前权限点的标识
    }
  },
  created () {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList () {
      const res = await getRoleList(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      // console.log(res)
    },

    // 回写员工信息
    async setRole (id) {
      let res = await getRoleDetail(id)
      this.roleForm = res.data
      this.showDialog = true
      // console.log(res)
    },

    // 修改员工
    async btnOk () {
      // 全部通过校验
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        await updataRole(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
      }
      this.getRoleList() // 重新拉取数据
      this.$message.success('操作成功')
      this.showDialog = false
    },

    // 关闭弹窗
    btnclose () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.showDialog = false
      this.$refs.roleForm.resetFields()
    },

    // 获取公司信息
    async getCompanyInfo () {
      let res = await getCompanyInfo(this.companyId)
      this.formData = res.data
      console.log(res)
    },
    changePage (newPage) {
      //newPage 当前点击页
      this.page.page = newPage
      this.getRoleList()
    },
    delRole (id) {
      this.$confirm('确认要删除该角色', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async () => {
          await delRole(id)
          this.getRoleList()
          this.$message.success('删除成功')
        })
        .catch(error => {
          console.log(error)
        })
    },

    // 分配权限点
    async btnPerm (id) {
      let res = await getPermissionList()
      // console.log(res)
      this.premData = tranListToTreeData(res.data, '0')
      // 记录当前点击的id
      this.userId = id
      let premRes = await getRoleDetail(id)
      // console.log(premRes)
      let premList = premRes.data.permIds
      this.selectCheck = premList
      this.premDialog = true
      // console.log(premList)
    },

    // 点击确定保存权限分配
    async btnPremOk () {
      // console.log(this.$refs.premTree.getCheckedKeys())
      await assignPerm({
        // getCheckedKeys 组件tree的实例方法 获取选中的节点返回一个数组
        permIds: this.$refs.premTree.getCheckedKeys(),
        id: this.userId
      })
      this.$message.success('分配权限成功')
      this.premDialog = false
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  }
}
</script>

<style>
.el-table {
  margin-top: 30px;
}
</style>
